<template>
  <div>
    <div v-show="!popShow">
      <div class="flexsa pt16 pl16 pr16">
        <div @click="seeMoreCourse">
          <img src="@/assets/icon/icon-njkt.png" alt="" />
          <div class="pt12">农技课程</div>
        </div>
        <div
          @click="$router.push({ name: 'agriNews', query: { active: 'a' } })"
        >
          <img src="@/assets/icon/icon-nqdt.png" alt="" />
          <div class="pt12">农情动态</div>
        </div>
        <div
          @click="
            $router.push({
              name: isExpert == 'specialist' ? 'OnlineReply' : 'OnlineAsk',
            })
          "
        >
          <img src="@/assets/icon/icon-zxdy.png" alt="" />
          <div class="pt12">
            {{ isExpert == "specialist" ? "在线答疑" : "专家答疑" }}
          </div>
        </div>
        <div @click="skip">
          <img src="@/assets/icon/specialist.png" alt="" />
          <div class="pt12">专家入驻</div>
        </div>
      </div>
      <div class="pl16 pr16">
        <div class="flexsb pt24 pb12 title-lesson">
          <div class="fz18" style="font-weight: bold">农技课程</div>
          <div style="color: #999999" @click="seeMoreCourse">
            <span>更多</span
            ><img class="ml8" src="@/assets/icon/nav-enter.png" alt="" />
          </div>
        </div>
        <div class="flexsb pb12">
          <div
            class="lesson-video"
            v-for="item in courseList"
            :key="item.id"
            @click="
              $router.push({
                name: 'VideoCourse',
                params: { courseId: item.id },
              })
            "
          >
            <div v-if="item.imgs" class="play-button"></div>
            <van-image :src="item.imgs" height="93" v-if="item.imgs">
              <template v-slot:loading>
                <van-loading type="spinner" size="20" />
              </template>
              <template v-slot:error>加载失败</template>
            </van-image>
            <div class="van-ellipsis mt10">{{ item.description }}</div>
          </div>
        </div>
      </div>
      <div class="line10px"></div>
      <div class="pl16 pr16 fz16">
        <div class="flexsb pt24 pb12 title-lesson">
          <div class="fz18" style="font-weight: bold">农事问题</div>
          <div
            style="color: #999999"
            @click="$router.push({ name: 'agriNews', query: { active: 0 } })"
          >
            <span>更多</span
            ><img class="ml8" src="@/assets/icon/nav-enter.png" alt="" />
          </div>
        </div>
        <div
          class="flexsb pb12 pt12 border-bottom-1"
          @click="
            $router.push({ name: 'agriNewsDetail', params: { id: item.id } })
          "
          v-for="item in dynamicList"
          :key="item.id"
        >
          <div class="text-container van-ellipsis">
            <div class="van-ellipsis mb10">{{ item.brief }}</div>
            <div class="fz12">{{ item.view }} 阅读</div>
          </div>
          <div class="img-container ml9" v-if="item.imgs">
            <van-image :src="item.imgs">
              <template v-slot:loading>
                <van-loading type="spinner" size="20" />
              </template>
              <template v-slot:error>加载失败</template>
            </van-image>
          </div>
        </div>
      </div>
      <div class="line10px"></div>
      <div class="pl16 pr16 fz16">
        <div class="flexsb pt24 pb12 title-lesson">
          <div class="fz18" style="font-weight: bold">政策解读</div>
          <div
            style="color: #999999"
            @click="$router.push({ name: 'agriNews', query: { active: 2 } })"
          >
            <span>更多</span
            ><img class="ml8" src="@/assets/icon/nav-enter.png" alt="" />
          </div>
        </div>
        <div
          class="flexsb pb12 pt12 border-bottom-1"
          @click="
            $router.push({ name: 'agriNewsDetail', params: { id: item.id } })
          "
          v-for="item in dynamicList1"
          :key="item.id"
        >
          <div class="text-container van-ellipsis">
            <div class="van-ellipsis mb10">{{ item.brief }}</div>
            <div class="fz12">{{ item.view }} 阅读</div>
          </div>
          <div class="img-container ml9" v-if="item.imgs">
            <van-image :src="item.imgs">
              <template v-slot:loading>
                <van-loading type="spinner" size="20" />
              </template>
              <template v-slot:error>加载失败</template>
            </van-image>
          </div>
        </div>
      </div>
    </div>

    <div class="msgBoxCont" v-show="!!popShow">
      <div
        class="msgBox"
        v-for="item in userInfo.roleList"
        :key="item.id"
        @click="handleClick(item)"
      >
        <img
          v-if="item.roleCode == 'declareDeclarant'"
          class="imageStyleS"
          src="../../assets/image/specialistIcon.png"
        />

        <img
          v-if="item.roleCode == 'sugarOffice'"
          class="imageStyleS"
          src="../../assets/image/specialist.png"
        />

        <img
          v-if="item.roleCode == 'city'"
          class="imageStyleS"
          src="../../assets/image/city.png"
        />

        <img
          v-if="item.roleCode == 'county'"
          class="imageStyleS"
          src="../../assets/image/county.png"
        />
        <img
          v-if="item.roleCode == 'sugarcaneGrower'"
          class="imageStyleS"
          src="../../assets/image/sugarcaneGrower.png"
        />
        <img
          v-if="item.roleCode == 'evaluationExpert'"
          class="imageStyleS"
          src="../../assets/image/evaluationExpert.png"
        />
        <span class="text-grey colorStyle title">{{ item.name }}</span>
      </div>
    </div>
  </div>
</template>
<script>
import Api from "@/services";
import { mapGetters } from "vuex";
import store from "@/store";
export default {
  name: "AgriculturalClass",
  data() {
    return {
      courseList: [],
      dynamicList: [],
      dynamicList1: [],
      userInfo: {},
      popShow: true,
    };
  },
  computed: {
    ...mapGetters(["isExpert"]),
    ...mapGetters(["getAppToken"]),
  },

  created() {
    if (store.state.token) {
      this.initCourseList();
    } else {
      let inter = setInterval(() => {
        if (store.state.token) {
          clearInterval(inter);
          this.initCourseList();
        }
      }, 200);
    }

    this.getTokenInfo();
  },
  methods: {
    handleClick(item) {
      store.commit("setRole", item.roleCode);
      this.popShow = false;
    },
    async getTokenInfo() {
      let param = new URLSearchParams();
      param.append("xSugarToken", this.getAppToken);
      param.append("type", 1);
      const result = await Api.getNewToken({
        data: param,
      });
      this.userInfo = result.userInfo;
      let resData = this.userInfo.roleList;
      if (resData.length == 1) {
        store.commit("setRole", resData[0].roleCode);
        this.popShow = false;
      }
    },
    async skip() {
      const res = await Api.getExpert({});
      if (res == null || res == undefined) {
        this.$router.push({ name: "ExpertsIn", params: { flag: true } });
      } else {
        this.$router.push({
          name: "submitSuccess",
          params: { state: res.checkStatus },
        });
      }
    },
    async seeMoreCourse() {
      try {
        const isColse = await Api.getCourseOpen();
        console.log("农机课堂是否关闭", isColse);
        if (!isColse) {
          this.$router.push({
            name:
              this.isExpert == "specialist"
                ? "ExpertVideoList"
                : "FarmerVideoList",
          });
        }
      } catch (e) {
        console.log(e.message);
        this.$toast({
          message: e.message,
          type: "fail",
          // duration:
        });
      }
    },
    async initCourseList() {
      let courseList;
      if (this.isExpert == "specialist") {
        courseList = await Api.getExpertCourseList({
          params: { flag: 0, pageSize: 10, currentPage: 1 },
        });
      } else {
        courseList = await Api.getFarmerCourseList({
          params: { type: 0, pageSize: 10, currentPage: 1 },
        });
      }
      const dynamicList = await Api.getAgriNews({ params: { colum: 0 } });
      const dynamicList1 = await Api.getAgriNews({ params: { colum: 2 } });
      this.courseList = courseList.list.slice(0, 2);
      this.dynamicList = dynamicList.slice(0, 2);
      this.dynamicList1 = dynamicList1.slice(0, 2);
    },
  },
};
</script>
<style lang="scss" scoped>
.flexsa {
  text-align: center;

  img {
    width: 45px;
    height: 45px;
  }
}

.title-lesson {
  img {
    width: 7px;
    height: 12px;
  }
}

.lesson-video {
  width: calc((100% - 13px) / 2);
  position: relative;

  .play-button {
    position: absolute;
    width: 36px;
    height: 36px;
    z-index: 10;
    border-radius: 50%;
    left: 50%;
    top: 46px;
    transform: translate(-50%, -50%);
    background-color: rgba(0, 0, 0, 0.5);

    &::before {
      content: "";
      position: absolute;
      left: 0;
      top: 0;
      width: 0;
      height: 0;
      border-top: 9px solid transparent;
      border-right: 15px solid transparent;
      border-bottom: 9px solid transparent;
      border-left: 15px solid rgba(255, 255, 255, 0.9);
      left: 50%;
      top: 50%;
      transform: translate(-16%, -50%);
    }
  }
}

.router-container {
  padding: 0 !important;
}

.text-container {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  justify-content: space-between;
}

.img-container {
  width: 113px;
  height: 63px;
  flex-shrink: 0;

  img {
    width: 100%;
    height: 100%;
  }
}

.margin-top-xl {
  margin-top: 5px;
}
.msgBox {
  padding: 0 20px;
  background-color: #f1f1f1;
  display: flex;
  flex-direction: column;
  height: 40px;
  line-height: 40px;
  border-radius: 20px;
  margin-top: 20px;
}

.msgBoxRight {
  margin-top: 30rpx;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.message {
  overflow: hidden;
  margin-top: 20px;
  border-radius: 50px;
  padding: 0px 20px;
}

.colorStyle {
  color: #000;
  margin-left: 20px;
}

.noMsg {
  text-align: center;
  margin-top: 20px;
}

.title {
  font-size: 14px;
  font-weight: bold;
}

.imageStyleS {
  width: 30px;
  height: 30px;
}
.msgBox {
  background-color: #f1f1f1;
  display: flex;
  flex-direction: row;
  margin-bottom: 10px;
  padding: 10px;
  border-radius: 20px;
  justify-content: center;
  align-items: center;
}

.msgBoxRight {
  margin-top: 10px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.message {
  overflow: hidden;
  margin-right: 15px;
  margin-left: 15px;
  border-radius: 10px;
  margin-top: 10px;
}

.colorStyle {
  color: #000;
  margin-left: 10px;
}

.noMsg {
  text-align: center;
  margin-top: 20px;
}

.title {
  font-size: 16px;
  font-weight: 500;
}

.msgBoxCont {
  padding-left: 16px;
  padding-right: 16px;
}
</style>
